package com.opengate.base.intercept;

import com.opengate.base.constants.AppConstants;
import com.opengate.base.utils.LogJsonToStrUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.MDC;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

/**
 * @author 曳戈泰尔
 * @version 1.0
 * @description HTTP请求日志切面
 * @date 2023/4/26 5:46 PM
 */
@Aspect
@Configuration
@Order(500)
@Slf4j
public class HttpLogAop {

    /** 切入点 */
    @Pointcut("execution(* *..controller..*Controller.*(..))")
    public void logPointCut() {}

    /** 前置通知 */
    @Before("logPointCut()")
    public void doBefore(JoinPoint joinPoint) {
        // 获取请求参数
        String reqStr = LogJsonToStrUtil.parseObjs(joinPoint.getArgs(), 500, 100).toString();
        // 获取请求头
        String authorization = MDC.get(AppConstants.AUTHORIZATION_HEADER);
        // 记录日志
        log.info("start：request={}，authorization={}", reqStr, authorization);
    }
}
